package com.abel.bigwater.dataserver.model.hgd

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.PacketNotEnoughException
import io.netty.buffer.Unpooled
import junit.framework.Assert.*
import org.junit.Test
import org.slf4j.LoggerFactory

class HgdUdpDataTests {
    @Test
    fun testUdp() {
        val data = HgdUdpData(HgdaoBase.TYPE_TCP)
        val buf = Unpooled.buffer().writeBytes(BcdString.fromBcdString(DEMO_HEX_STRING).hexArray)
        assertTrue(data.parseBuffer(buf, true))

        lgr.info(data.toString())
        lgr.info(data.channelList.joinToString())

        val d2 = HgdUdpData()
        try {
            d2.parseBuffer(buf, false)
            fail("should not enough!")
        } catch (ex: PacketNotEnoughException) {
            lgr.info("good exception~")
        }
    }

    @Test
    fun testAlmostFull() {
        val buf = Unpooled.buffer().writeBytes(BcdString.fromBcdString(DEMO_HEX_STRING.dropLast(15)).hexArray)
        val d2 = HgdUdpData()
        try {
            d2.parseBuffer(buf, false)
            fail("should not enough!")
        } catch (ex: PacketNotEnoughException) {
            lgr.info("good exception~")
        }
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(HgdUdpDataTests::class.java)

        const val DEMO_HEX_STRING = """AB CD 00 7D 00 66 11 05 24 16 15 03 0F 02 CE 00 05
        1E 44 31 31 35 3B 31 2E 34 3B 31 2E 30 2E 31 31 20 20 20 20 20 00 00 00 00 00 00 00 00
        01 01 0B 01 00 00 37 07 00 00 38 07 00 00 48 07 00 00
        01 02 0B 00 00 00 01 02 00 00 03 02 00 00 05 02 00 00
        02 03 01 01 00
        03 04 01 01 00
        04 05 3E 4C CC CD 3E 80 00 00 3E 8F 5C 29
        05 0D 00 12 00 13 40 11
        06 0E 00 12 67 89 00 12 64 89 00 32 67 80
        6A 0D 0A AB CD"""
    }
}